package com.yubest.security.service.impl;

import cn.hutool.core.util.StrUtil;
import com.yubest.security.bean.UserInfo;
import com.yubest.security.consts.Consts;
import com.yubest.security.service.PermissionService;
import com.yubest.security.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service(value = "ps")
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private TokenService tokenService;

    @Override
    public boolean hasPermit(String code) {
        if (StrUtil.isBlank(code)) {
            return true;
        }
        UserInfo userInfo = tokenService.getCurrentUser();
        if (null == userInfo || null == userInfo.getPermitCodeList()) {
            return false;
        }
        return userInfo.getPermitCodeList().contains(Consts.PERMIT_ALL)
                || userInfo.getPermitCodeList().contains(code);
    }
}
